#########################################################################################################
# Cox Proportional Hazard Model with Time-Varying Covariates
# ----------------------------------------------------------
# This script estimates a stratified Cox proportional hazard model 
# with time-varying covariates. Following the methodology of Seru et al. (2010), each purchase of a stock 
# is treated as the start of a new position. A position is considered to end on the date the investor first 
# sells part or all of their holdings.

# Output:
# -------
# **Table A34: Cox Proportional Hazard Model Estimates of the Stocks Disposition Effect (Week-Peak)**
# - `tables/cox_MAXC_peak_5update_sellsample.tex`
#########################################################################################################


data.reg.used<-data_for_peaks[,.(anon, port_date, sell, gain.since.pur, gain.since.point, Code_used_DS, pur_day)] 

data.reg.used[,count_obs:=seq_along(port_date), by=.(anon, Code_used_DS, pur_day)]

# Flag the first sale date for each stock purchase
data.reg.used[sell==1, keep_date:=min(port_date), by=.(anon, Code_used_DS, pur_day)]
data.reg.used[, flag:=0]
data.reg.used[, flag2:=0]
data.reg.used[port_date==keep_date, flag:=1]

data.reg.used[,seqsell:=cumsum(sell), by=.(anon, Code_used_DS, pur_day)]

# Identify the last observation date for each stock purchase

data.reg.used[,max.date:=max(port_date), by=.(anon, Code_used_DS, pur_day)]
data.reg.used[port_date==max.date & seqsell==0, flag2:=1]
data.reg.used[, flag_all:=0]
data.reg.used[, flag_all:=ifelse(flag==1, 1, ifelse(flag2==1,1,0))]

# Filter to keep only the first sale or last observation records

data.reg.used[,keeping_to_event:=cumsum(flag_all), by=.(anon, Code_used_DS, pur_day)]
data.reg.used[keeping_to_event==1 & flag_all==1, keeping_to_event:=0] 

data.reg.used<- data.reg.used[keeping_to_event==0]

# The above code retains observations until the first sale (either partial or complete) of holdings.

data.reg.used[,elapsed:=as.numeric(port_date-pur_day)]
data.reg.used[ , date_yest:=shift(port_date,1,NA,"lag"),by=.(anon, Code_used_DS, pur_day)]

data.reg.used[,elapsed_start:=as.numeric(date_yest-pur_day)]
data.reg.used<- data.reg.used[,.(anon, port_date, sell, gain.since.pur, gain.since.point, Code_used_DS, pur_day, elapsed, 
                                 elapsed_start, date_yest, pur_day)] 

data.reg.used[is.na(elapsed_start) & is.na(date_yest), elapsed_start:=0]


library(survival)

summary(m1c <- coxph(Surv(elapsed_start, elapsed, sell) ~ gain.since.pur + strata(anon) + cluster(anon)  , 
                     data = data.reg.used))

summary(m2c <- coxph(Surv(elapsed_start, elapsed, sell) ~ gain.since.point   + strata(anon) + cluster(anon)  , 
                     data = data.reg.used))
summary(m3c <- coxph(Surv(elapsed_start, elapsed, sell) ~ gain.since.pur + gain.since.point + strata(anon) + cluster(anon)  , 
                     data = data.reg.used))


important_labels<- c( "Gain Since Purchase=1",
                     "Gain Since Peak=1")


table<-stargazer( m1c, m2c, m3c,
                  title="", align=TRUE,type="latex",
                  dep.var.labels=c(""),
                  covariate.labels=important_labels,
                  omit.stat=c("LL","ser","f", "adj.rsq") , 
                  omit.table.layout = "n",
                  float=F, 
                  table.layout ="-dc#-tas-",
                  style = "aer",
                  column.sep.width = "10pt",
                  digits=4 
)

setwd(wd_tables)

library(stringr)
t <- str_replace_all(table, "\\^", "")
t <- str_replace_all(t, "R\\$\\{2\\}\\$", "R$^{2}$")


write.table(t[10:(length(t)-6)], col.names = F, 
            row.names = F, quote = FALSE, paste0("cox_", plot_name, "_", use_days ,"sample.tex")) 









